package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.instructionalMatters.project.VO.ClassroomVO;
import edu.zqu.instructionalMatters.project.entity.Classroom;
import edu.zqu.instructionalMatters.project.mapper.ClassroomMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.FieldTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课室 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课室")
public class ClassroomController {
    @Autowired
    private ClassroomMapper classroomMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private FieldTypeService fieldTypeService;

    @ApiOperation(value = "获取课室列表")
    @GetMapping(value = "/classrooms")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldTypeId", value = "场地类型id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             Integer fieldTypeId) {
        Classroom classroom1 = new Classroom().setCollegeId(collegeId).setFieldTypeId(fieldTypeId); //合成 Classroom 对象
        List<Classroom> list1 = classroomMapper.findAll(classroom1);                                //获取 Classroom 列表
        List<ClassroomVO> list = new ArrayList<>();                                                 //初始化 ClassroomVOList
        for (Classroom classroom : list1) {                                                          //循环赋值
            ClassroomVO classroomVO = new ClassroomVO();                                            //初始化 ClassroomVO
            BeanUtils.copyProperties(classroom, classroomVO);                                        //对象复制赋值
            String collegeName = collegeService.getCollegeName(classroom.getCollegeId());           //获取学校名称
            String fieldTypeName = fieldTypeService.getFieldTypeName(classroom.getFieldTypeId());   //获取场地类型名称
            classroomVO.setCollegeName(collegeName);                                                //设置学校名称
            classroomVO.setFieldTypeName(fieldTypeName);                                            //设置场地类型名称
            list.add(classroomVO);                                                                  //classroomVO 加入 list
        }
        return new ZQUResult(200, "请求成功", list);                                      //成功返回
    }

    @ApiOperation(value = "获取课室信息", response = ClassroomVO.class)
    @GetMapping(value = "/classroom/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getOne(@PathVariable BigInteger id) {
        Classroom classroom = classroomMapper.selectById(id);                                   //获取课室对象
        String collegeName = collegeService.getCollegeName(classroom.getCollegeId());           //获取学校名称
        String fieldTypeName = fieldTypeService.getFieldTypeName(classroom.getFieldTypeId());   //获取场地类型名称
        ClassroomVO classroomVO = new ClassroomVO();                                            //获取ClassroomVO对象
        BeanUtils.copyProperties(classroom, classroomVO);                                        //对象复制赋值
        classroomVO.setCollegeName(collegeName);                                                //设置学校名称
        classroomVO.setFieldTypeName(fieldTypeName);                                            //设置场地类型名称
        return new ZQUResult(200, "请求成功", classroomVO);                           //成功数据返回
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/classroom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldTypeId", value = "场地类型id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "seatCount", value = "座位数", paramType = "query", required = false),
            @ApiImplicitParam(name = "numRow", value = "行数", paramType = "query", required = false),
            @ApiImplicitParam(name = "numColumn", value = "列数", paramType = "query", required = false),
            @ApiImplicitParam(name = "maxVolumn", value = "最大容纳人数", paramType = "query", required = false),
            @ApiImplicitParam(name = "availableVolumn", value = "当前可容纳人数", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger fieldId,
                         Integer fieldTypeId,
                         String name,
                         String englishName,
                         Integer seatCount,
                         Integer numRow,
                         Integer numColumn,
                         Integer maxVolumn,
                         Integer availableVolumn,
                         BigInteger byWho) {
        Classroom classroom = new Classroom();               //准备新纪录数据模型
        classroom.setCollegeId(collegeId);                   //设置学校id
        classroom.setFieldId(fieldId);                       //设置场地id
        classroom.setFieldTypeId(fieldTypeId);               //设置场地类型id
        classroom.setName(name);                             //设置课室名称
        classroom.setEnglishName(englishName);               //设置课室英文名称
        classroom.setSeatCount(seatCount);                   //设置学生座位数
        classroom.setNumRow(numRow);                         //设置行数
        classroom.setNumColumn(numColumn);                   //设置列数
        classroom.setMaxVolumn(maxVolumn);                   //设置最大容纳人数
        classroom.setAvailableVolumn(availableVolumn);       //设置当前可容纳人数
        classroom.setByWho(byWho);                           //设置操作者
        classroomMapper.insert(classroom);                   //执行插入新纪录
        return ZQUResult.success();                          //操作成功返回
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/classroom/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "学期id", paramType = "path", required = true),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldTypeId", value = "场地类型id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "seatCount", value = "座位数", paramType = "query", required = false),
            @ApiImplicitParam(name = "numRow", value = "行数", paramType = "query", required = false),
            @ApiImplicitParam(name = "numColumn", value = "列数", paramType = "query", required = false),
            @ApiImplicitParam(name = "maxVolumn", value = "最大容纳人数", paramType = "query", required = false),
            @ApiImplicitParam(name = "availableVolumn", value = "当前可容纳人数", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger fieldId,
                            Integer fieldTypeId,
                            String name,
                            String englishName,
                            Integer seatCount,
                            Integer numRow,
                            Integer numColumn,
                            Integer maxVolumn,
                            Integer availableVolumn,
                            BigInteger byWho) {
        Classroom classroom = classroomMapper.selectById(id);//先查出旧记录
        classroom.setByWho(byWho);                           //修改操作者
        classroomMapper.updateById(classroom);               //更新操作者
        classroomMapper.deleteById(id);                      //逻辑删除旧记录
        classroom = new Classroom();                         //准备新纪录数据模型
        classroom.setCollegeId(collegeId);                   //设置学校id
        classroom.setFieldId(fieldId);                       //设置场地id
        classroom.setFieldTypeId(fieldTypeId);               //设置场地类型id
        classroom.setName(name);                             //设置课室名称
        classroom.setEnglishName(englishName);               //设置课室英文名称
        classroom.setSeatCount(seatCount);                   //设置学生座位数
        classroom.setNumRow(numRow);                         //设置行数
        classroom.setNumColumn(numColumn);                   //设置列数
        classroom.setMaxVolumn(maxVolumn);                   //设置最大容纳人数
        classroom.setAvailableVolumn(availableVolumn);       //设置当前可容纳人数
        classroom.setByWho(byWho);                           //设置操作者
        classroom.setLastVersion(id);                        //设置上一个版本号
        classroomMapper.insert(classroom);                   //执行插入新纪录
        return ZQUResult.success();                          //操作成功返回
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/classroom/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学期id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        Classroom classroom = classroomMapper.selectById(id);//先查出来
        classroom.setByWho(byWho);                           //修改操作者
        classroomMapper.updateById(classroom);               //更新操作者
        classroomMapper.deleteById(id);                      //逻辑删除
        return ZQUResult.success();                          //操作成功返回
    }
}

